package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Region;
import cn.wolfcode.wolf2w.query.RegionQueryObject;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IRegionService;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


@RequestMapping("region")
@Controller
public class RegionController {

    @Autowired
    private IRegionService regionService;

    @Autowired
    private IDestinationService destinationService;

    /**
     * 页面展示
     * @param model
     * @param qo
     * @return
     */
    @RequestMapping("/list")
    public String list(Model model,@ModelAttribute("qo") RegionQueryObject qo){
        //查询页面数据
        Page page = regionService.query(qo);
        //分享数据到页面
        model.addAttribute("page", page);

        return "region/list";
    }

    /**
     * 回显目的地下拉框数据
     * @return
     */
    @RequestMapping("/listDests")
    @ResponseBody
    public Object listDests(){
        return destinationService.list();
    }

    /**
     * 新增或编辑
     * @param region
     * @return
     */
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Region region){

        regionService.saveOrUpdate(region);
        return JsonResult.success();
    }

    /**
     * 查看关联目的地
     * @return
     */
    @RequestMapping("/getDestByRegionId")
    @ResponseBody
    public Object getDestByRegionId(String rid){

        List<Destination> list = destinationService.queryDestByRegionId(rid);
        return list;
    }

    /**
     * 修改区域热门
     * @return
     */
    @RequestMapping("/changeHotValue")
    @ResponseBody
    public Object changeHotValue(int hot , String id){

        regionService.changeHotValue(hot,id);
        return JsonResult.success();
    }

    /**
     * 删除
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(String id){
        regionService.delete(id);
        return JsonResult.success();
    }

    /**
     * 回显目的地下拉框数据
     * @return
     */
    @RequestMapping("/getDestByDeep")
    @ResponseBody
    public Object getDestByDeep(int deep){
        return destinationService.getDestByDeep(deep);
    }

}
